import {messages} from "share/common";
import React,{ Component } from 'react'
import { connect } from 'react-redux'
import { Button, message } from 'antd'
import SetSearchAreaForm from '../set-search-area-form';
import SetTableColumns from '../set-table-columns'
import menuRoute from "routes/menuRoute";
import OrderCenterServer from "../order-center-server";
import OrderCenterUtil from '../order-center-util';
import FictitiousProgress from '../components/fictitious-progress';

class ListPlane extends Component{

  constructor(props){
    super(props);
    this.searchParams = {// 获取表格数据默认是参数
      channel: "hly-admin",
      searchType: 'hlytime',
      companyOID: props.company.companyOID,
    }
    this.columns = SetTableColumns.getHotel();
    this.state = {
      data:[],
      loading: false,
      consumerPlaneDetail:menuRoute.getRouteItem('consumer-plane-detail','key'),
      searchFormData:[],// 搜所配置项
      showDownloadModal: false,// 下载前生成文件进度条
      isCreated: false, // 是否已经生成
      downloadUrl: '',
      isFail: false,
    }
  }

  componentWillMount(){
    const searchForms = SetSearchAreaForm.getHotelForm(this);
    this.setState({
      searchFormData: searchForms,
    })
  }

  createTableOption = () => {
    const {searchFormData} = this.state;
    return {
      title: messages('supplier-4.key182')/*酒店订单*/,
      key: 'hotel',
      service: this.advanceTableServer,
      index: true,
      onRef: this.advanceTableRef,
      rowKey: record => record.companyCode,
      columns: this.columns,
      searchForm: searchFormData,
      footer: <Button type='primary' onClick={this.downloadExcel}>{messages('supplier-4.key233')/*导出搜索数据*/}</Button>,
      bordered: true,
      onRow: (record) => {
        return {
          onClick: () => {
            // this.onJumpDetail(record);
          }
        };
      }
    }
  }

  advanceTableRef = (ref) => {
    this.tableRef = ref;
  }

  advanceTableServer = (page, size, searchValues) => {
    this.searchParams = Object.assign({},this.searchParams, searchValues, {page: page+1, size: size});
    return OrderCenterServer(this).getHotelData(this.searchParams);
  }

  /**
   * 跳转详情
   * @param record 表格被点击行数据
   */
  onJumpDetail = (record) => {
    this.context.router.push(this.state.consumerPlaneDetail.url.replace(':recordId',record.orderId));
  }

  // 导出表格
  downloadExcel = () => {
    this.setState({
      showDownloadModal: true,
      isFail: false
    });
    OrderCenterServer(this).exportOrderCenterData(this.searchParams,'hotel').then(res => {
      if(res && res.data.exportFileRecord){
        this.setState({
          downloadUrl: res.data.exportFileRecord.tempUrl,
          isCreated: true,
        })
      }else{
        this.setState({
          isFail: true,
        });
        this.onCancelDownload();
        message.error(res.data.response.message);
      }
    })
  }

  /**
   * 取消下载
   */
  onCancelDownload = () => {
    this.setState({
      showDownloadModal: false,
      isCreated: false,
      downloadUrl: '',
    })
  }

  render(){
    return (
      <div>
        <div style={{height: 'calc(100vh - 114px)'}}>
          {OrderCenterUtil.orderCenterTable(this)}
        </div>
        <FictitiousProgress onCancel={this.onCancelDownload} {...this.state}/>
      </div>
    )
  }
}

ListPlane.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    company: state.login.company,
  }
}

export default connect(mapStateToProps)(ListPlane);
